#!/usr/bin/env python
"""
# -*- coding: utf-8 -*-
# @Time    : 2020/10/13 13:00
# @Author  : AshJo
# @Desc    : 进程池和线程池
# @Filename: tp_pool.py
# @Version : v1.0
"""

import os
import time
import random
from threading import current_thread
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor


# 线程池
def func():
    print(current_thread().ident, 'start')
    time.sleep(random.randint(1, 4))
    print(current_thread().ident, 'end')


if __name__ == '__main__':
    tp = ThreadPoolExecutor(4)
    for i in range(20):
        tp.submit(func)

# 进程池
# def func(a, b):
#     print(os.getpid(), 'start', a, b)
#     time.sleep(random.randint(1, 4))
#     print(os.getpid(), 'end')
#     return a * b
#
#
# if __name__ == '__main__':
#     tp = ProcessPoolExecutor(4)
#     future_l = {}
#     for i in range(20):
#         ret = tp.submit(func, i, b=i + 1)
#         future_l[i] = ret
#     for key in future_l:
#         print(key, future_l[key].result())
